import requests, json, os, time, csv, pickle
import multiprocessing as mp

f = open("temp/number_service.csv", "w", encoding="utf-8", newline="")
wf = csv.writer(f)

def downloader(epodoc):
	# Posting Accesstoken from EPO
	url = "https://ops.epo.org/3.2/auth/accesstoken"
	payload = "grant_type=client_credentials"
	headers = {
	'Content-Type': 'application/x-www-form-urlencoded',
	'Authorization': 'Basic ZUdJcVlpZWxaZWZTRjJSWkFFOXNCSnIwc2dpRWhGeVc6d0dkWWsyWVF5Unhpa05nbA==',
	'Content-Type': 'application/x-www-form-urlencoded'
	}
	while True:
		try:
			response = requests.request("POST", url, data=payload, headers=headers, timeout = (10, 30))
			break
		except:
			pass
	time.sleep(0.3)
	response = response.text
	response_json  = {}
	if response != "":
		response_json = json.loads(response)
	token = response_json["access_token"]

	# Getting Nunbers for each application number
	url = "http://ops.epo.org/3.2/rest-services/number-service/publication/epodoc/"+epodoc+"/original".format(epodoc)
	headers = {
	'Content-Type': 'application/x-www-form-urlencoded',
	'Authorization': 'Bearer {}'.format(token),
	'Content-Type': 'application/x-www-form-urlencoded'
	}
	while True:
		try:
			response = requests.request("GET", url, data=payload, headers=headers, timeout = (10,30))
			break
		except:
			pass
	time.sleep(0.3)
	xml = response.text

	# parsing, saving
	soup = Soup(xml, "xml")
	epodoc = xml[:-4]
	try:
		original = soup.find("document-id", {"document-id-type": "original"})
		country = original.find("country").text.strip()
		doc_number = original.find("doc-number").text.strip()
		kind = original.find("kind").text.strip()
		date = original.find("date").text.strip()
		row = [epodoc, country, doc_number, kind, date]
		wf.writerow(row)
		print(row)
	except :
		pass


# Multi-Processing Mapping
if __name__ == '__main__':
	epodoc_list = []
	with open("raw/equivalents.csv", "r", encoding="utf-8") as f:
		rf = csv.reader(f)
		for line in rf:
			if line[2][:2]=="KR" or line[2][:2]=="US":
				epodoc_list.append(line[2]) 
	pool = mp.Pool(50)
	pool.map(downloader, epodoc_list)
	pool.close()